import os
import shutil
import time
import traceback
from datetime import datetime


def auto_delete():
    folder = r'D:\blue_caches'
    current_time = time.time()
    time_threshold = current_time - 2 * 24 * 3600
    for item in os.listdir(folder):
        if item == 'autotest_env':
            continue
        item_path = os.path.join(folder, item)
        item_mtime = os.path.getmtime(item_path)

        if item_mtime < time_threshold:
            try:
                if os.path.isfile(item_path) or os.path.islink(item_path):
                    os.remove(item_path)
                    print(f'已删除文件：{item_path}')
                elif os.path.isdir(item_path):
                    shutil.rmtree(item_path)
            except:
                print('删除失败：{}，错误：{}'.format(item_path, traceback.format_exc()))


if __name__ == '__main__':
    while True:
        print(datetime.now())
        auto_delete()
        time.sleep(24 * 3600)
